package com.cogent.dynamicform.form.impl;

import java.util.HashMap;

import com.cogent.business.helper.DistrictHelper;
import com.cogent.common.util.MapUtil;
import com.cogent.dynamicform.domain.BaseFormColumnDomain;
import com.cogent.dynamicform.domain.FormColumnDomain;
import com.cogent.dynamicform.form.AbstractDynamicForm;
import com.cogent.expression.facade.ExpressionFacade;
import com.opensymphony.oscache.util.StringUtil;

public class DistrictSelectDynamicFormImpl extends AbstractDynamicForm {
	
	public String genQueryField(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		StringBuffer str = new StringBuffer();
		str.append("<select name=\"").append(formColumnDomain.getName()).append("\" id=\"").append(formColumnDomain.getName()).append("\" ");
		if(!StringUtil.isEmpty(formColumnDomain.getJs()))
			str.append(formColumnDomain.getJs());
		str.append(">");
		boolean flag = StringUtil.isEmpty(formColumnDomain.getTypecode());
		String curvalue = ExpressionFacade.getExpressionValue(formMap, formColumnDomain.getCalformula());
		boolean islimit = true;
		if(StringUtil.isEmpty(curvalue)) {
			islimit = false;
			curvalue = MapUtil.getStringValue(formMap, formColumnDomain.getName(), ExpressionFacade.getExpressionValue(formMap, formColumnDomain.getExpression()));
		} else {
			formMap.put(formColumnDomain.getName(), curvalue);
		}
		str.append(DistrictHelper.getSubDistOption(formMap, MapUtil.getStringValue(formMap, formColumnDomain.getTypecode(), 
					ExpressionFacade.getExpressionValue(formMap, formColumnDomain.getExpression())), curvalue, flag, islimit, formColumnDomain.getName()));
		str.append("</select>");
				
		return str.toString();
	}
	
	public String genInputField(FormColumnDomain formColumnDomain, HashMap formMap) {
		if(formColumnDomain.getViewmodel() > 0)
			return getShowValue(formColumnDomain, formMap) + "&nbsp;";
		StringBuffer str = new StringBuffer();
		str.append("<select name=\"").append(formColumnDomain.getName()).append("\" id=\"").append(formColumnDomain.getName()).append("\" ");
		if(formColumnDomain.getJs().length()>0)
			str.append(formColumnDomain.getJs());
		str.append(" ").append(formColumnDomain.getShowcontrol()).append(">");
		boolean flag = StringUtil.isEmpty(formColumnDomain.getTypecode());
		String name = formColumnDomain.getName();
		if(name.indexOf(".") > 0)
			name = name.substring(name.indexOf(".") + 1);
		String curvalue = ExpressionFacade.getExpressionValue(formMap, formColumnDomain.getCalformula());
		boolean islimit = true;
		if(StringUtil.isEmpty(curvalue)) {
			islimit = false;
			curvalue = MapUtil.getStringValue(formMap, name, ExpressionFacade.getExpressionValue(formMap, formColumnDomain.getExpression()));
		} else {
			formMap.put(name, curvalue);
		}
		str.append(DistrictHelper.getSubDistOption(formMap, MapUtil.getStringValue(formMap, formColumnDomain.getTypecode(), 
					ExpressionFacade.getExpressionValue(formMap, formColumnDomain.getExpression())), curvalue, flag, islimit, name));
		str.append("</select>");
		if(formColumnDomain.isIsrequired())
			str.append("<span class=\"txt_red\">&nbsp;*</span>");
		
		return str.toString();
	}
	
	public String getShowValue(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		if(formColumnDomain.getShowtype() == 1)
			return "&nbsp;";
		String name = formColumnDomain.getName();
		if(name.indexOf(".") > 0)
			name = name.substring(name.indexOf(".") + 1);
		HashMap curMap = DistrictHelper.getDistByID(MapUtil.getStringValue(formMap, name));
		return MapUtil.getStringValue(curMap, "districtname", "&nbsp;");
	}
}
